// pages/search/index.js
import { request } from "../../request/request.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs:[{
      id:0,
      value:"综合",
      isActive:true


    },{
        id: 1,
        value: "销量",
        isActive: false

    },{
        id: 2,
        value: "价格",
        isActive:false

    }

    ],
    goodsList:[],

  },
  queryParams:{
    query:"",
    cid:"",
    pagenum:1,
    pagesize:10

  },
  totalPages:1,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryParams.cid=options.cid||"";
    this.queryParams.query=options.query||"";
    
    this.getGoodsList();
    wx.showLoading({
      title: '加载中',
    })
    

  },
  async getGoodsList(){
    const res = await request({ url:"/goods/search",date:this.queryParams});
    const total=res.data.message.total;
    this.totalPages=Math.ceil(total/this.queryParams.pagesize);
    console.log(this.totalPages);
    this.setData({
      goodsList:[...this.data.goodsList,...res.data.message.goods]
    })
    wx.stopPullDownRefresh()
  },
  tabsItemChange(e){
   const index=e.detail.index;
   let tabs=this.data.tabs;
   tabs.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false);
   this.setData({
     tabs:tabs
   });



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      goodsList:[]
    });
    this.queryParams.pagenum=1;
    
    this.getGoodsList()

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.queryParams.pagenum>=this.totalPages)
    {
      wx-wx.showToast({
        title: '已经是最后一页了',
        
        success: (res) => {},
      })
    }
    else{
      this.queryParams.pagenum++;
      this.getGoodsList();


    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})